package com.zho.util;

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import org.apache.commons.lang.StringUtils;
import org.springframework.security.authentication.encoding.Md5PasswordEncoder;

import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import java.io.*;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.security.spec.InvalidKeySpecException;

/**
 * Helpers class, which contains static methods helper methods like loading the
 * given property file etc.
 */
public final class StringHelpers {

	// for logging
	private static final Logit log = Logit.getInstance(StringHelpers.class);

	// singleton
	private StringHelpers() {
		;
	}

	public static List<String> separaterStringBySymbol(String source, String symbol) {
		List<String> list = null;
		String token = null;
		try {
			if (StringUtils.isNotEmpty(source)) {
				list = new ArrayList<String>();
				StringTokenizer st = new StringTokenizer(source, symbol);
				while (st.hasMoreElements()) {
					token = st.nextToken();
					list.add(token);
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
			log.error(e.getMessage(), e);
		}

		return list;
	}
	
    public static String encodeMD5String(String str) {
    	Md5PasswordEncoder encode = new Md5PasswordEncoder();
        return encode.encodePassword(str, null);
    }
    
    
	public static String encryptString(String input) throws Exception {
//			NoSuchPaddingException, InvalidKeyException, IllegalStateException,
//			IllegalBlockSizeException, BadPaddingException,
//			UnsupportedEncodingException, InvalidKeySpecException {
//		SecretKey key = getHardCodedKey();
//		Cipher cipher = Cipher.getInstance("DESede");
//		cipher.init(Cipher.ENCRYPT_MODE, key);
//		byte[] inputBytes = input.getBytes();
//		byte[] encryptedBytes = cipher.doFinal(inputBytes);
//		String encryptedString = new sun.misc.BASE64Encoder()
//				.encode(encryptedBytes);
//		return URLEncoder.encode(encryptedString, "UTF-8");
		return input;
	}

   public static String decryptString(String encryptedString) throws Exception {
		// TODO Auto-generated method stub
//		SecretKey key = getHardCodedKey();
//		String ka = URLDecoder.decode(encryptedString, "UTF-8");
//		byte[] encryptedBytes = new sun.misc.BASE64Decoder().decodeBuffer(ka);
//		Cipher cipher = Cipher.getInstance("DESede");
//		cipher.init(Cipher.DECRYPT_MODE, key);
//		byte[] recoveredBytes = cipher.doFinal(encryptedBytes);
//		String recoveredString = new String(recoveredBytes);

		return encryptedString;
	}

	public static SecretKey getHardCodedKey() throws InvalidKeyException,
			NoSuchAlgorithmException, InvalidKeySpecException,
			InvalidKeySpecException {
		byte[] keyBytes = { 37, -51, -14, -113, -2, 124, -50, -125, -2, 118,
				28, 94, -45, -3, -125, 22, 93, 81, 91, -53, 124, 28, 31, 127 };
		DESedeKeySpec keySpec = new DESedeKeySpec(keyBytes);
		SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");
		SecretKey key = keyFactory.generateSecret(keySpec);
		return key;
	}
    
    public static void main(String[] args) {
		// System.out.println(encodeString("5FD9DBC285"));
		try {
			
			String out1 = encryptString("CUS110000");
			System.out.println("Encrypt = " + out1);
			String out2 = decryptString("AaFx1TWZX0U=");
			System.out.println("Decrypt = " + out2);
		} catch (Exception e) {
			// TODO: handle exception
		}
    }
}
